// TUI-CustomerService/pages/index.js
import TencentCloudChat from '@tencentcloud/chat';
import TIMUploadPlugin from 'tim-upload-plugin';
import TIMProfanityFilterPlugin from 'tim-profanity-filter-plugin';
import { genTestUserSig } from './debug/GenerateTestUserSig';
var app = getApp();
Component({

  /**
   * 页面的初始数据
   */
  data: {
    config: {
      userID: '', // User ID
      //SECRETKEY: '105aded4e3a12e8f0575de45004b3875dfde47447ae8aef8c622dedd5f8505b5', // Your secretKey
      //SDKAPPID: 1600024735, // Your SDKAppID
      SECRETKEY:'2c9e8d20a55db4a964ac9167639ce5e16a3d0903cb870e68a88f61f6c2b4d07c',
      SDKAPPID:1600048147,
      EXPIRETIME: 604800,
    },
    type:'',
    groupid:'',
    GLchat:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options,'--------------options')
    this.setData({
      GLchat:options.GLchat,
      type:options.type,
      groupid:options.groupID
    })
    //this.getUserID()
  },
  attached(){
    console.log('11111111111111111111111111111')
    //this.getUserID()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  methods:{
    onSDKReady(){
      const TUIKit = this.selectComponent('#TUIKit');
      TUIKit.init();
    },
    getUserID(){
      console.log('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
      const _this = this
      let urlStr = app.getNetAddresss("tencent.im.importUser");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: this.data.dataJson,
        success: function (resdata) {
          var res = resdata.data;
          console.log(res,'--------------sdfsdfsdfs')
          if(res.result === 1) {
            _this.setData({
              'config.userID':res.data
            })
            wx.$TUIKit = TencentCloudChat.create({
              SDKAppID: _this.data.config.SDKAPPID,
            });
            app.globalData.TUIKit =  wx.$TUIKit;
            const userSig = genTestUserSig(_this.data.config).userSig 
            wx.$chat_SDKAppID = _this.data.config.SDKAPPID;
            wx.TencentCloudChat = TencentCloudChat;
            wx.$chat_userID = _this.data.config.userID;
            wx.$chat_userSig = userSig;
            wx.$TUIKit.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin });
            wx.$TUIKit.registerPlugin({ 'tim-profanity-filter-plugin': TIMProfanityFilterPlugin });
            wx.$TUIKit.login({
              userID: _this.data.config.userID,
              userSig
            });
  
            // 监听系统级事件
            //wx.$TUIKit.on(wx.TencentCloudChat.EVENT.SDK_READY, _this.onSDKReady,_this);
            const TUIKit = _this.selectComponent('#TUIKit');
            TUIKit.init();
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          clickTap = 0;
        }
      });
    }
  },
  
})